package com.example.onion.hxchat.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.SpannedString;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.example.onion.hxchat.room.entity.User;
import com.example.onion.hxchat.thread.PlayUserDetailThread;
import com.example.onion.hxchat.thread.PlayUserImg;
import com.example.onion.hxchat.util.HttpUtils;
import com.example.onion.hxchat.util.PassWordHelp;
import com.example.superyi.myteam.R;

import java.io.IOException;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.FormBody;
import okhttp3.Request;

/**
 * @author:onion
 */
public class AlterUserInfoActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout imgLayout;
    private LinearLayout nameLayout;
    private LinearLayout sexLayout;
    private LinearLayout emailLayout;
    private LinearLayout mottoLayout;

    private CircleImageView circleImageView;
    private EditText nameTv;
    private EditText yearTv;
    private EditText emailTv;
    private EditText spyTv;
    private Button alterBtn;
    private String accountId;

    private final String url = "";//更新信息的url

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_userinfo);
        accountId = PassWordHelp.readPassword(AlterUserInfoActivity.this)[0];
        new Thread(new PlayUserImg("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2534506313,1688529724&fm=26&gp=0.jpg", handler)).start();
        new Thread(new PlayUserDetailThread(accountId, handler)).start();
        initView();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case PlayUserImg.PLAYIMG_IS_FAILED:
                    break;
                case PlayUserImg.PLAYIMG_IS_FINISH:
                    circleImageView.setImageIcon(Icon.createWithBitmap((Bitmap) msg.obj));
                    break;
                case PlayUserDetailThread.DETAIL_IS_FINISH:
                    setContent((User) msg.obj);
                    break;
                default:
                    break;
            }
        }
    };

    @SuppressLint("SetTextI18n")
    private void setContent(User user) {
        Log.e("userDetailActivity", user.toString());
        nameTv.setHint(user.getUserName());
        yearTv.setHint(user.getUserGrade());
        spyTv.setHint(user.getUserSpy());
        emailTv.setHint(user.getUserEmail());
//        mottoTv.setText(user.get);
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        alterBtn = findViewById(R.id.alter_user_info_btn);
        alterBtn.setOnClickListener(this);
        imgLayout = findViewById(R.id.alter_user_info_img_layout);
        imgLayout.setOnClickListener(this);
        nameLayout = findViewById(R.id.alter_user_info_name_layout);
        nameLayout.setOnClickListener(this);
        sexLayout = findViewById(R.id.alter_user_info_year_layout);
        sexLayout.setOnClickListener(this);
        emailLayout = findViewById(R.id.alter_user_info_email_layout);
        emailLayout.setOnClickListener(this);

        circleImageView = findViewById(R.id.alter_user_info_img_civ);
        nameTv = findViewById(R.id.alter_user_info_name_tv);
        SpannableString s = new SpannableString("你好呀 小美人");//这里输入自己想要的提示文字
        nameTv.setHint(new SpannedString(s));
        yearTv = findViewById(R.id.alter_user_info_year_tv);
        yearTv.setHint(new SpannedString(s));
        emailTv = findViewById(R.id.alter_user_info_email_tv);
        emailTv.setHint(new SpannedString(s));
        spyTv = findViewById(R.id.alter_user_info_spy_tv);
        spyTv.setHint(new SpannedString(s));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.alter_user_info_img_layout://修改头像layout
                //修改头像逻辑
                break;
            case R.id.alter_user_info_name_layout:
                //修改昵称逻辑
                break;
            case R.id.alter_user_info_year_layout:
                //修改年级逻辑
                break;
            case R.id.alter_user_info_email_layout:
                //修改邮箱逻辑
                break;
            case R.id.alter_user_info_spy_layout:
                //修改专业逻辑
                break;
            case R.id.alter_user_info_btn:
                //修改按钮
                String newName = nameTv.getText().toString().trim();
                String year = yearTv.getText().toString().trim();
                String email = emailTv.getText().toString().trim();
                String spy = spyTv.getText().toString().trim();

                HttpUtils httpUtils = HttpUtils.getInstance();
                FormBody.Builder builder = new FormBody.Builder();
                builder.add("userId", accountId);
                builder.add("userName", newName);
                builder.add("userGrade", year);
                builder.add("userEmail", email);
                builder.add("userSpy", spy);
                FormBody formBody = builder.build();
                httpUtils.asyncPost(url, formBody, new HttpUtils.HttpCallBack() {
                    @Override
                    public void onError(Request request, IOException e) {
                        showToast(AlterUserInfoActivity.this,"修改失败，请检查网络问题!");
                    }

                    @Override
                    public void onSuccess(Request request, String result) {

                    }
                });
                break;
            default:
                break;
        }
    }

    /**
     * 显示toast
     * @param ctx
     * @param msg
     */
    public static void showToast(final Activity ctx, final String msg) {
        // 判断是在子线程，还是主线程
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
        } else {
            // 子线程
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
                }
            });
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home://系统自带的返回箭头的资源id
                finish();
                Log.d("AlterUserInfoActivity", "返回 ");
                break;

            default:
                break;
        }
        return true;
    }

}
